package com.ccys.cms.controller.openApi;

import com.ccys.common.constants.TouristApi;
import com.ccys.common.enums.CodeEnum;
import com.ccys.common.util.JedisUtil;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.entity.TblExchangeRate;
import com.ccys.db.entity.TblHopAiaVitalityAttribute;
import com.ccys.db.entity.TblS3RootFolder;
import com.ccys.db.mapper.TblExchangeRateMapper;
import com.ccys.db.mapper.TblHopAiaVitalityAttributeMapper;
import com.ccys.db.mapper.TblS3RootFolderMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: OpenApiController
 * @Description:
 * @Date: 2024/3/8 09:44
 */
@RestController
@Api("wch开放接口")
@RequestMapping("/openApi")
public class OpenApiController {

    @Resource
    private TblExchangeRateMapper exchangeRateMapper;

    @Resource
    private TblHopAiaVitalityAttributeMapper hopAiaVitalityAttributeMapper;

    @Resource
    private TblS3RootFolderMapper rootFolderMapper;

    @GetMapping("/getExchangeRate")
    @ApiOperation("获取汇率")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ori", value = "待转换货币", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "tar", value = "目标货币", required = true, dataType = "string", paramType = "query"),
    })
    @TouristApi
    public ResponseResultApp<TblExchangeRate> getExchangeRate(@Param("ori")String ori,@Param("tar")String tar){
        Kit.requisiteParam("ori","tar");
        TblExchangeRate rate = new TblExchangeRate();
        rate.setBaseName(ori);
        rate.setRateName(tar);
        rate.setDelFlag(0);
        return ResponseResultApp.success(exchangeRateMapper.selectOne(rate));
    }

    @GetMapping("/getHopAttrList")
    @ApiOperation("获取全部hop属性")
    @ApiImplicitParams({
    })
    @TouristApi
    public ResponseResultApp<List<TblHopAiaVitalityAttribute>> getHopAttrList(){
        //做5分钟缓存
        Object hopAttrs = JedisUtil.getObject("hopAttrs", CodeEnum.REDIS_THIRD.codeOf());
        if(hopAttrs!=null){
            return ResponseResultApp.success((List<TblHopAiaVitalityAttribute>)hopAttrs);
        }
        TblHopAiaVitalityAttribute condition = new TblHopAiaVitalityAttribute();
        condition.setDelFlag(0);
        List<TblHopAiaVitalityAttribute> list = hopAiaVitalityAttributeMapper.select(condition);
        if(list.isEmpty()){
            return ResponseResultApp.success();
        }
        JedisUtil.setexObj("hopAttrs",5*60,list, CodeEnum.REDIS_THIRD.codeOf());
        return ResponseResultApp.success();
    }

    @GetMapping("/getRootFolderList")
    @ApiOperation("获取全部s3路径")
    @ApiImplicitParams({
    })
    @TouristApi
    public ResponseResultApp<List<TblS3RootFolder>> getRootFolderList(){
        TblS3RootFolder condition = new TblS3RootFolder();
        condition.setDelFlag(0);
        return ResponseResultApp.success(rootFolderMapper.select(condition));
    }


}
